
/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module batchtab.c - Tab related functions for Batch File Maintenance |
   @end

$Log: /SoftPay/SPK2SIM/Source/Batch/batch.c $
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/

#include <project.h>

#ifdef BATCH_DEFINED

#define BATCHTAB_C

#include <aclconio.h>
#include <stdlib.h>
#include <stdio.h>
#include <ascii.h>
#include <errno.h>
#include <svc.h>
#include <dbmgr.h>
#include <string.h>
#include <message.h>
#include <aclstr.h>
#include <aclutil.h>
#include <VarRec.h>

/** /#define DEBUG_BATCHTAB*/
#ifdef DEBUG_BATCHTAB
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <tran.h>
#include <util.h>
#include <mem.h>
#include <gds.h>
#include <msg.h>
#include <msgfuncs.h>
#include <msgclrk.h>
#include <msgshf.h>
#include <udt.h>
#include <cdt.h>
#include <cst.h>
#include <hdt.h>
#include <bdt.h>
#include <opt.h>
#include <tct.h>
#include <trt.h>
#include <sdt.h>
#include <udt.h>
#include <confio.h>
#include <form.h>
#include <revutl.h>
#include <test.h>
#include <spdebug.h>
#include <ui.h>
#include <entry.h>
#include <operate.h>
#include <pbatch.h>
#include <mdlfuncs.h>
#include <accum.h>
#include <table.h>
#include <funcids.h>
#include <autosttl.h>
#include <settle.h>
#include <settlbat.h>
#include <review.h>
#include <batmsg.h>
#include <mht.h>
#include <pfr.h>
#include <codesec.h>
#include <logo.h>
#include <mit.h>
#include <merch.h>
#include <multilng.h>
#include <oper.h>
#include <date.h>
#include <tblutil.h>

#define TAB_BATCH_FNAME "TAB"

/*********************************************************************************************
* @func void | vdGetOpenTabBatchFName |
*
*       Loads the open tab batch file name.
*
* @parm char *  | pchBFName |
*
* @rdesc VOID.
* @end
**********************************************************************************************/

void vdGetOpenTabBatchFName(char* pchBFName) {
    /* This name attribution suggest a batch file per Merchant instead of per Host */

    /** /pdebug(("--vdGetOpenTabBatchFName--"));*/
    sprintf(pchBFName, "%s%02d", TAB_BATCH_FNAME, inGetMerchantNum());
}

/*********************************************************************************************
* @func void | vdBatchOTOpen |
*
*       Open Tab batch.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc VOID.
* @end
**********************************************************************************************/

void vdBatchOTOpen(TRANSACTION_OBJECT* pobTran) {
    inOpenTabBatch(pobTran);
}

/*********************************************************************************************
* @func void | vdCloseTabBatch |
*
*       This closes the Open Tab batch.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc VOID
* @end
**********************************************************************************************/

void vdCloseTabBatch(TRANSACTION_OBJECT* pobTran) {
    inCloseTabBatch(pobTran);
} /* End of vdCloseTabBatch () */

/*********************************************************************************************
* @func int | inOpenTabBatch |
*
*       This opens the TAB batch, and loads the Search Key Compare Function.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

int inOpenTabBatch(TRANSACTION_OBJECT* pobTran) {
    char szBatchFileName[FILENAME_SIZE + 1];

    if (!pobTran->fOTBatchOpen) {
        /** /pdebug (("--inOpenTabBatch--"));*/
        vdGetOpenTabBatchFName(szBatchFileName);

        if (inOpenBatch(&pobTran->dbOTBatch, szBatchFileName) != VS_SUCCESS) {

            vdSGErrorMessage(BATCH_ERR_MSG);
            return(VS_ERR);
        }

        pobTran->fOTBatchOpen = VS_TRUE;

        /* Load the batch compare function */
        pobTran->srBSKey.compare_func = inCompareFn;
    }

    return (VS_SUCCESS);
}

/*********************************************************************************************
* @func int | inCloseTabBatch |
*
*       This closes the TAB batch.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc return values.
* @flag int |  VS_ERR or VS_SUCCESS
* @end
**********************************************************************************************/
int inCloseTabBatch(TRANSACTION_OBJECT* pobTran) {
    int inResult = VS_SUCCESS;

    if (pobTran->fOTBatchOpen) {
        if (inCloseBatch(&pobTran->dbOTBatch) != VS_SUCCESS) {

            vdSGErrorMessage(BATCH_ERR_MSG);
            inResult = VS_ERR;
        }
        pobTran->fOTBatchOpen = VS_FALSE;
    }

    return (inResult);
} /* End of inCloseTabBatch () */

/*********************************************************************************************
* @func static int | inGetOBatchRec |
*
*       retrieve Open Tab records from Open Tab batch by record number.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
* @parm int     | inRecNum |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

static int inGetOBatchRec(TRANSACTION_OBJECT* pobTran,int inRecNum) {
    /** /pdebug(("--inGetOBatchRec--"));*/
    if (inReadBatchData(&pobTran->dbOTBatch, &pobTran->srBKey, &pobTran->srBRec, (long) inRecNum) != VS_SUCCESS) {

        vdSGErrorMessage(BATCH_ERR_MSG);
        return(VS_ERR);
    }

    return(VS_SUCCESS);
}

int inGetOTKeyBatchRec(TRANSACTION_OBJECT* pobTran,int inKey,BATCH_KEY* psrBatchKey,long lnDBWhat) {
    return (inGetLinkedKeyBatchRec(pobTran, inKey, psrBatchKey, lnDBWhat, &pobTran->dbOTBatch));
}


/*********************************************************************************************
* @func int | inLoadOBatchRec |
*
*       Loads specific batch record from the Open Tab batch based on record
*       number.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
* @parm int     | inRecNum |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

int inLoadOBatchRec(TRANSACTION_OBJECT* pobTran,int inRecNum) {
    if (inGetOBatchRec(pobTran, inRecNum) != VS_SUCCESS) {

        vdSGErrorMessage(BATCH_ERR_MSG);
        return(VS_ERR);
    }

    if (inLoadCardCurrenyHostData(pobTran) != VS_SUCCESS)
        return(VS_ERR);

    pobTran->fLanguageOnSwipe = VS_TRUE;
    return(VS_SUCCESS);
}

/*********************************************************************************************
* @func int | inGetOTInvBatchRec |
*
*       Loads a batch record based on invoice number from Open Tab batch
*
* @parm TRANSACTION_OBJECT *    | pobTran |
* @parm int    | inKey |
* @parm long    | lnDBWhat |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

int inGetOTInvBatchRec(TRANSACTION_OBJECT* pobTran,int inKey,long lnDBWhat) {
    return (inGetInvBatchRec(pobTran, inKey, lnDBWhat, &pobTran->dbOTBatch));
}

/*********************************************************************************************
* @func int | inGetOBatchRecCnt |
*
*       Gets the count of record in the Open Tab batch file.
*       Precondition:
*           The cdt host index should be set to the appropriate value.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc Batch count.
* @end
**********************************************************************************************/

int inGetOBatchRecCnt(TRANSACTION_OBJECT* pobTran) {
    VS_BOOL fCloseBatch;
    int inRecCnt;

    fCloseBatch = (VS_BOOL) (!pobTran->fOTBatchOpen);
    vdBatchOTOpen(pobTran);
    inRecCnt = (int) pobTran->dbOTBatch.kfile.num_of_recs;
    if (fCloseBatch)
        vdCloseTabBatch(pobTran);
    /** /pdebug (("inRecCnt=%d",inRecCnt));*/

    return(inRecCnt);
}

/*********************************************************************************************
* @func int | inPutPreAuthBatRec |
*
*       Transfers a batch record from pre-auth batch record to the corresponding
*       Online batch (for ISO).
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

int inPutPreAuthBatRec(TRANSACTION_OBJECT* pobTran) {
    long lnInvNum;
    long lnLast4;

    /** /pdebug(("--inPutPreAuthBatchRec--"));*/

    /*
     *  07/02/97: The host handle "call" replaced a cdt index "call" -
     *  currency index should always be used though.  Look
     *  into at a later date; for now, let alone.
     */

    /*
     *  Load Open Tab Invoice Number for delete search
     *  First save Close Tab Invoice # for PutBatchRec
     *  Load original Open Tab's acct. # for DeleteOBatchRec
     */
    lnInvNum = pobTran->srBKey.lnInvNum;
    pobTran->srBKey.lnInvNum = pobTran->lnOTInvNum;

    if (pobTran->srBRec.fCloseTabNewCard) {
        lnLast4 = pobTran->srBKey.lnLast4;
        pobTran->srBKey.lnLast4 = pobTran->lnOTAcctNum;
    }
    if (inDeleteOBatchRec(pobTran) != VS_SUCCESS) {

        vdSGErrorMessage(BATCH_ERR_MSG);
        return(VS_ERR);
    }
    vdCloseTabBatch(pobTran);

    pobTran->srBKey.lnInvNum = lnInvNum;
    if (pobTran->srBRec.fCloseTabNewCard)
        pobTran->srBKey.lnLast4 = lnLast4;
    if (inPutBatchRec(pobTran) != VS_SUCCESS)
        return(VS_ERR);

    return(VS_SUCCESS);
}

/*********************************************************************************************
* @func int | inSettleOpenTabs |
*
*       Check Open Tab Settle flag and deletes the open tabs or
*       returns an ESCAPE to abort settlement.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

int inSettleOpenTabs(TRANSACTION_OBJECT* pobTran) {
    int inResult;
    signed char chAnswer = 0x00;
    char szMsg1[MAX_CHARS_PER_ROW + 1];
    char szMsg2[MAX_CHARS_PER_ROW + 1];
    int inMaxCharsPerRow = inGetRowLim();
    int inMsg1Row = obDisp.inFirstLineCOne;
    char* chNull = 0;

    if ((inResult = inCheckOpenTabs(pobTran)) != VS_SUCCESS) {
        /* open tabs exist - check for autosettle */

        if ((fGGAutoSettle()) && (fGetOTabSettle() == VS_FALSE)) {
            vdCloseTabBatch(pobTran);
            return(inResult);
        } else if (!fGGAutoSettle()) /* manual settlement */ {
            vdClearNonTitleLines();
            if (getgrid() == 0)
                vdCalculateCoord(GRID_4x8_TO_8x21, &inMaxCharsPerRow, &inMsg1Row);
            if (inGetMITTotRecs() > 1) {
                strlcpy(szMsg1, szGetMerchantText(), inMaxCharsPerRow);
                vdDisplayAt_ASC8X21(obDisp.inHomeCol, inMsg1Row, szMsg1, NO_CLEAR);
            }
            vdGetMessageFromFile(DELETE_OPEN_TAB_MSG15, szMsg2);
            vdDisplayAt(obDisp.inHomeCol, obDisp.inSecondLineCOne, szMsg2, NO_CLEAR);

            chAnswer = (char) obDisp.inDO_ChooseOne(chNull, chNull, (char *) pchGetMsg(YESNO_MSG5x), 2, NULL_PFI);

            if (chAnswer == KEY_YES) {
                inResult = inDeleteOpenTabBatch(pobTran);
                if (inResult != VS_SUCCESS)
                    return(inResult);
                else /* clear display from bottom of the screen */ {
                    SVC_WAIT(1000);
                    vdClearStatusMessage();
                }
            } else if ((chAnswer == (char) VS_ESCAPE) || (chAnswer == (char) KEY_NO)) {
                vdCloseTabBatch(pobTran);
                vdSGErrorMessage(0L);
                return(VS_ESCAPE);
            }
        } /* end not autosettle or manual settle*/
    }
    return(inResult);
}

/*********************************************************************************************
* @func int | inCheckOpenTabs |
*
*       Check Open Tab Settle flag and deletes the open tabs or
*       returns an ESCAPE to abort settlement.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

int inCheckOpenTabs(TRANSACTION_OBJECT* pobTran) {
    /* modified function to incorporate bug fixes from 2.5 base 09/23/99 gw */
    /*char chAnswer = 0x00;*/

    if (fOpenTabEmpty(pobTran) != VS_TRUE)  /* Open tab batch is not empty */ {
        vdSGErrorMessage(OPEN_TABS_EXIST_MSG21);
        return(VS_ESCAPE);
    }
    vdCloseTabBatch(pobTran);

    return(VS_SUCCESS);
}


/*********************************************************************************************
* @func int | inDeleteOpenTab |
*
*       Deletes the open tab.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

int inDeleteOpenTab(TRANSACTION_OBJECT* pobTran) {
    if (inDeleteOBatchRec(pobTran) != VS_SUCCESS) {

        vdSGErrorMessage(BATCH_ERR_MSG);
        return(VS_ERR);
    }

    vdDispMsgCentered(DELTAB_SUCCESS_MSG21, obDisp.inInputLine, CLR_LINE);
    vdDelay(2 * PROMPT_DELAY);

    return(VS_SUCCESS);
}

/*********************************************************************************************
* @func int | inDeleteOBatchRec |
*
*       Searches tab batch record based on invoice number plus
*       account number, and deletes record from tab batch.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

int inDeleteOBatchRec(TRANSACTION_OBJECT* pobTran) {
    int inResult,
    inRetVal = VS_SUCCESS;
    long lnRecNum,
    lnRetVal;
    BATCH_KEY* psrLocalBKey;
    BATCH_REC* psrLocalBRec;

    /** /pdebug(("--inDeleteOBatchRec-%ld-",pobTran->srBKey.lnInvNum));*/

    if (inGetLocalBatchStructures(&psrLocalBKey, &psrLocalBRec) != VS_SUCCESS)
        return VS_ERR;

    /*
     *      We need to read the current record from the tab batch with db_get_key() to
     *      obtain the correct record number to delete.
     */
    pobTran->srBSKey.inKeyIdx = KEY_INV_NUM + KEY_ACC_NUM;
    memcpy(&pobTran->srBSKey.BKey, &pobTran->srBKey, sizeof(BATCH_KEY));

    if ((lnRetVal = lnBatchGetKey(&pobTran->dbOTBatch, psrLocalBKey, &pobTran->srBSKey, DB_FIRST, psrLocalBRec, &lnRecNum)) == 1L) {
        if ((inResult = db_delete(&pobTran->dbOTBatch, lnRecNum, 1L)) < 0) {
            /** /pdebug (("DelOBatRec %d/%ld/%d",inResult,lnRecNum,errno));*/
            inRetVal = VS_ERR;
        }
    } else {
        /** /pdebug (("DelOBatRec %ld/%d",lnRetVal,errno));*/
        inRetVal = VS_ERR;
    }

    vdFreeLocalBatchStructures(psrLocalBKey, psrLocalBRec);
    return inRetVal;
}

/*********************************************************************************************
* @func int | inDeleteOpenTabBatch |
*
*       Clear transactions from the Open Tab batch.
*
* @parm TRANSACTION_OBJECT *    | pobTran |
*
* @rdesc Return values.
*
* @flag int     |   - VS_SUCCESS
*                   - VS_ERR
* @end
**********************************************************************************************/

int inDeleteOpenTabBatch(TRANSACTION_OBJECT* pobTran) {
    char pchBFName[FILENAME_SIZE + 1];

    /** /pdebug(("--inDeleteOpenTabBatch--"));*/
    if (fOpenTabEmpty(pobTran) != VS_TRUE) {
        vdDisplayMessageFromFile(DELETE_OPEN_TAB_BATCH_MSG21, STATUS_MSG);
        vdCloseTabBatch(pobTran);
        vdGetOpenTabBatchFName(pchBFName);
        if (db_open(&pobTran->dbOTBatch, pchBFName, SIZE_BATCH_KEY, (O_TRUNC | O_CREAT | O_RDWR)) < 0)
            return(VS_ERR);
        pobTran->fOTBatchOpen = VS_TRUE;
    }
    vdCloseTabBatch(pobTran);
    return(VS_SUCCESS);
}

int BATCHTAB_CODE_CHECK(char* a,char* b,short * c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}

#undef BATCHTAB_C

#endif // BATCH_DEFINED
